from django.db import models
from luffy.utils.models import BaseModel
from users.models import User
from courses.models import Course, CourseTime


# Create your models here.
class Order(BaseModel):
    status_choices = (
        (0, '未支付'),
        (1, '已支付'),
        (2, '已取消'),
        (3, '超时取消'),
    )
    pay_choices = (
        (0, '支付宝'),
        (1, '微信支付'),
    )
    order_title = models.CharField(max_length=150, verbose_name='订单标题')
    total_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='订单总价', default=0)
    real_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='实付金额', default=0)
    order_number = models.CharField(max_length=64, verbose_name='订单号')
    order_status = models.SmallIntegerField(choices=status_choices, default=0, verbose_name='订单状态')
    pay_type = models.SmallIntegerField(choices=pay_choices, default=0, verbose_name='支付方式')
    use_coupon = models.BooleanField(default=False, verbose_name='是否使用优惠券')
    coupon = models.IntegerField(null=True, verbose_name='用户优惠券ID')
    order_desc = models.TextField(null=True, blank=True, max_length=500, verbose_name='订单描述')
    pay_time = models.DateTimeField(null=True, verbose_name='支付时间')
    user = models.ForeignKey(User, related_name='user_orders', on_delete=models.DO_NOTHING, verbose_name='下单用户')

    class Meta:
        db_table = 'ly_order'
        verbose_name = '订单记录'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '{}, 总价：{},实付：{}'.format(self.order_title, self.total_price, self.real_price)

    def order_status_text(self):
        return self.status_choices[self.order_status][1]

class OrderDetail(BaseModel):
    order = models.ForeignKey(Order, related_name='order_courses', on_delete=models.CASCADE, verbose_name='订单')
    course = models.ForeignKey(Course, related_name='course_orders', on_delete=models.CASCADE, verbose_name='课程')
    expire = models.IntegerField(default='-1', verbose_name='有效期周期')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='课程原价')
    real_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='课程实价')
    discount_name = models.CharField(max_length=120, default='', verbose_name='优惠类型')

    class Meta:
        db_table = 'ly_order_detail'
        verbose_name = '订单详情'
        verbose_name_plural = verbose_name

    def course_img(self):
        return self.course.course_img.url

    def expire_text(self):
        if self.expire == -1:
            return '永久有效'
        else:
            coursetime = CourseTime.objects.get(timer=self.expire, course=self.course)
            return coursetime.title

    def course_name(self):
        return self.course.name
